package com.stox.shared.watchlist.model;

import com.stox.shared.core.model.Scrip;
import com.stox.shared.core.model.intf.HasScrip;
import com.stox.shared.core.model.intf.Identifiable;
import com.stox.shared.core.util.StringUtil;

public class WatchlistEntry implements Identifiable, HasScrip{

	private Long id;
	
	private transient Scrip scrip;
	
	private final Long scripId;
	
	private final Long watchlistId;
	
	public WatchlistEntry(Long scripId, Long watchlistId) {
		this.scripId = scripId;
		this.watchlistId = watchlistId;
	}
	
	public WatchlistEntry(String text) {
		String[] tokens = text.split(",");
		if(0 < tokens.length && StringUtil.hasText(tokens[0])) setId(Long.parseLong(tokens[0]));
		if(1 < tokens.length && StringUtil.hasText(tokens[1])) this.scripId = Long.parseLong(tokens[1]); else throw new IllegalArgumentException();
		if(2 < tokens.length && StringUtil.hasText(tokens[2])) this.watchlistId = Long.parseLong(tokens[2]); else throw new IllegalArgumentException();
	}

	@Override
	public String toString() {
		return getId()+","+getScripId()+","+getWatchlistId();
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getScripId() {
		return scripId;
	}

	public Long getWatchlistId() {
		return watchlistId;
	}

	public Scrip getScrip() {
		return scrip;
	}
	
	public void setScrip(Scrip scrip) {
		this.scrip = scrip;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((scripId == null) ? 0 : scripId.hashCode());
		result = prime * result
				+ ((watchlistId == null) ? 0 : watchlistId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		WatchlistEntry other = (WatchlistEntry) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (scripId == null) {
			if (other.scripId != null)
				return false;
		} else if (!scripId.equals(other.scripId))
			return false;
		if (watchlistId == null) {
			if (other.watchlistId != null)
				return false;
		} else if (!watchlistId.equals(other.watchlistId))
			return false;
		return true;
	}
	
}
